<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="e" uri="/easytogo-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title></title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">
	<e:base/>
	<script type="text/javascript">
        setBodyOnLoad(doEventHandler);
        var t;
        function doEventHandler(){
            t = uiManager.getObject("leftTree");
            t.setOnClickHandler(function(id){
                var url = t.getUserData(id,"url");
	            $("#contextDiv").html("URL:"+url);
        	});
////////////右键菜单事件
			var menu = t.getContextMenu();   
	        //添加
	        menu.addEventListener("addNode",function(id){   
	            //alert("[自定义事件] 在父节点[ "+id+" ]上添加节点"+t.getLevel(id));   
	            //promptInfo('友好交互', '请输入您的答案', function(r){if(r){alertInfo('信息','您输入了:'+r);}});   
	        	var url="${pageContext.request.contextPath}/admin/resource_toNew.action?admin=${admin}";
				$.ajax({
					type: "post",
					url: url,
					data: {"pid":id,"level":t.getLevel(id)},
					dataType:"html",
					success: function(msg){
						  $("#contextDiv").html(msg);
				  			//document.getElementById("div_1").innerHTML=msg;
					  			
					}
				}); 
	        });   
	        //删除
	        menu.addEventListener("deleteNode",function(id){
				if(id=='1'||id=='2'){
					alert("此节点不能删除");
					return;
				}
	            //alert("[自定义事件] 要删除节点：" + id);   
	            confirmInfo('删除确认', '您确定删除此节点及其子节点?', function(r){
	            	if(r){
	            		var url="${pageContext.request.contextPath}/admin/resource_delete.action";
						$.ajax({
							type: "post",
							url: url,
							data: {"id":id},
							dataType:"text",
							success: function(msg){
								if(msg=="failed"){
									alert("删除失败!");
								}else{
									if("${successAlert}"=="1"){
										alert("删除成功!");
									}
									t.deleteNode(id);
								}
								//location.reload();
							}
						}); 
	            	}
	            });   
	        });   
	        //修改
	        menu.addEventListener("nodeRename",function(id){
	            //alert("[自定义事件] 要重命名节点：" + id);   
	            var url="${pageContext.request.contextPath}/admin/resource_toUpdate.action";
				$.ajax({
					type: "post",
					url: url,
					data: {"id":id},
					dataType:"html",
					success: function(msg){
						  $("#contextDiv").html(msg);
				  			//document.getElementById("div_1").innerHTML=msg;
					  			
					}
				}); 
	        });   
	           
	        var moveId = "";
	        var movePid = "";
	        var moveLevel = "";
	        var moveDirc = "";
	        menu.addEventListener("nodeMoveUp",function(id){   
	            //alert("节点上移：" + id);  
	            moveDirc = 1;
	            return true;//返回值false，则终止节点移动   
	        });   
	           
	        menu.addEventListener("nodeMoveDown",function(id){   
	            //alert("节点下移：" + id);  
	            moveDirc = 2;
	            return true;//返回值false，则终止节点移动   
	        });   
	           
	        menu.addEventListener("nodeMoveLeft",function(id){   
	            //alert("节点左移：" + id); 
	            moveDirc = 3;
	            return true;//返回值false，则终止节点移动   
	        });   
	           
	        menu.addEventListener("nodeMoveRight",function(id){   
	            //alert("节点右移：" + id);   
	            moveDirc = 4;
	            return true;//返回值false，则终止节点移动   
	        });
	        t.setDragHandler(function(id, toid){
            	//alert("[doDrag event]  level: " + t.getLevel(id) + " ;id:" + id + " ;toid:" + toid + " ;pid:" + t.getParentId(id));   
		        moveId=id;
            	moveLevel = t.getLevel(toid)+1;//移动之后的级别
            	movePid = toid;//移动之后的pid
            	//alert(moveId+"-"+movePid+"-"+moveLevel+"-"+moveDirc);
            	var url="${pageContext.request.contextPath}/admin/resource_move.action";
				$.ajax({
					type: "post",
					url: url,
					data: {"id":moveId,"pid":movePid,"level":moveLevel,"dirc":moveDirc},
					dataType:"text",
					success: function(msg){
						if(msg=="failed"){
							alert("移动失败!");
						}
						//location.reload();
					}
				}); 
            	return true; //返回值false则终止拖拽   
		    });   
        }
        
        //提交添加功能请求,此方法为new.jsp页面的事件响应函数
        function sub_new(){
			var pid=$("#pid").val();
			var name=$("#name").val();
			var id=$("#id").val();
        	var url="${pageContext.request.contextPath}/admin/resource_saveNew.action";
        	if(id!=null&&id!=""){//如果有id说明是更新
        		url="${pageContext.request.contextPath}/admin/resource_saveUpdate.action";
        	}
        	$.ajax({
				type: "post",
				url: url,
				data: {"id":id,"pid":pid,"level":$("#level").val(),"name":name,"url":$("#url").val(),"admin":$("#admin").val()},
				dataType:"text",
				success: function(msg){
					if(msg=="failed"){
						alert("保存失败!");
					}else{
						if("${successAlert}"=="1"){
							alert("保存成功!");
						}
						if(id!=null&&id!=""){//如果是更新的话直接更新树中显示内容
							t.setNodeText(id, name);
						}else{//否则添加一个新的
							t.addNode(pid, msg, name); 
						}
						$("#contextDiv").html("");
					}
					//location.reload();
				}
			}); 
        }
    </script>
  </head>
  
  <body>
	  <div style="height: 100%;width: 30%;float: left;">
		  <e:tree id="leftTree" treeLoadUrl="${pageContext.request.contextPath }/common/tree_getTreeData.action?admin=${admin}" height="100%" 
		  	isShowContextMenu="true" menuLoadUrl="${pageContext.request.contextPath }/admin/resource/contextMenu.xml" 
		  	menuImagePath="${pageContext.request.contextPath }/easytogo/ui/tree/dhtmlxtree/image/contextMenu/" isEnableDrag="false"
		  	/>
	  </div>
	  <div id="contextDiv" style="height: 100%;width: 50%;float: left;">
	  </div>
  </body>
</html>
